package swd.work.generator.v1.java.base;

import java.io.*;
import java.util.*;
import swd.base.SwdBase;

import com.pub.database.*;

public class PageExtend extends SwdBase {
    protected final String gstrTabChar = ""; //要使用何種字元當內縮字元ex." ","\t",etc,.

    protected StringBuffer gstbTagTabStr = new StringBuffer(); //共內縮幾個gstrTabChar
    public String gstrUserNO; //要取得那一組環境參數
    public String gstrSysNO;
    public boolean gblnMultiLang;
    public String gstrPageURI; //Page 的執行路徑..ex http://.......
    public String gstrOutPath; //程式輸出路徑
    public String[] gstrColorArray; //css 名稱


    protected PageExtend(QueryBean pqbnDAO, String pstrUserNO) throws Exception {
        super(null,null,pqbnDAO);
        try {
            gstrUserNO = pstrUserNO;
            gstrSysNO = super.getSysNO(gstrUserNO).toLowerCase();
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }

    /**
     * getColorArray
     *
     * @return String[]
     */
    private String[] getColorArray(String pstrPrgNO) {
        String lstrColor[] = new String[3];
        String lstrColorSet = new String();
        try {
            lstrColorSet = ((ArrayList<Object>) gqbnDAO.querySQL(
                    "select color_set from wkf_frm0 where prg_no='" + pstrPrgNO +
                    "' and seqno='0001' order by prg_no,seqno").get(0)).get(0).
                           toString();

            switch (Integer.parseInt(lstrColorSet)) {
            case 0:
                lstrColor[0] = "table_head";
                lstrColor[1] = "td_head";
                lstrColor[2] = "td_content";
                break;
            case 1:
                lstrColor[0] = "head_green";
                lstrColor[1] = "content_green";
                lstrColor[2] = "content_green_white";
                break;
            case 2:
                lstrColor[0] = "head_orange";
                lstrColor[1] = "content_orange";
                lstrColor[2] = "content_orange_white";
                break;
            case 3:
                lstrColor[0] = "head_purple";
                lstrColor[1] = "content_purple";
                lstrColor[2] = "content_purple_white";
                break;
            default:
                lstrColor[0] = "table_head";
                lstrColor[1] = "td_head";
                lstrColor[2] = "td_content";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return lstrColor;
    }

    /**
     * loadMultiLang
     *
     */
    private boolean getMultiLang() throws Exception {
        ArrayList<Object> laltWKF_INI_FILE = null;
        boolean lblnMultiLang = false;

        try {
            laltWKF_INI_FILE = super.loadDataFromWKF_INI_FILE("multi_yn",
                    gstrUserNO);

            if (laltWKF_INI_FILE != null && laltWKF_INI_FILE.get(0) != null) {
                lblnMultiLang = laltWKF_INI_FILE.get(0).toString().
                                equals("Y");
            } else {
                lblnMultiLang = false;
                throw new Exception("load multi-lang error!!");
            }
        } catch (Exception e) {
            lblnMultiLang = false;
            throw new Exception(e.toString());
        } finally {
            laltWKF_INI_FILE.clear();
            laltWKF_INI_FILE = null;
        }
        
        return lblnMultiLang;
    }

    /**
     * getOutPath
     *
     * @return String
     * @param pstrRunFrame String
     * @throws Exception
     */
    private String getOutPath(String pstrRunFrame) throws Exception {
        return super.loadDataFromWKF_INI_FILE(pstrRunFrame + "_prg_path",
                                              gstrUserNO).get(0).toString();
    }

    /**
     * getPageURI
     *
     * default use jsp_run_path,if append any frame work must change the method.
     *
     * @return String
     */
    private String getPageURI() throws Exception {
        ArrayList<Object> laltWKF_INI_FILE = null;

        try {
            laltWKF_INI_FILE = super.loadDataFromWKF_INI_FILE(
                    "apserver_url,apserver_port,sys_no", gstrUserNO);

            return laltWKF_INI_FILE.get(0).toString() + ":" +
                    laltWKF_INI_FILE.get(1).toString() + "/" +
                    laltWKF_INI_FILE.get(2).toString().toUpperCase();

        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.toString());
        } finally {
            laltWKF_INI_FILE.clear();
            laltWKF_INI_FILE = null;
        }
    }

    protected int getFormMaxColSize(String pstrPrgNO, String pstrFrmNO) throws
            Exception {
        return Integer.parseInt(gqbnDAO.readSingleValueData("select max(col_size) from (select count(*) as col_size from wkf_frm1 where prg_no='" +
                pstrPrgNO + "' and frm_no='" + pstrFrmNO +
                "' and rw_mode<>'H' group by fld_row_pos)"));
    }

    protected void loadCommData(String pstrPrgNO, String pstrRunFrame) throws
            Exception {
        gblnMultiLang = getMultiLang();
        gstrPageURI = getPageURI();
        gstrOutPath = getOutPath(pstrRunFrame);
        gstrColorArray = getColorArray(pstrPrgNO);
    }

    /**
     * 用來輸出含縮排的Tag
     * @param pbwrOutput BufferedWriter 輸出物件
     * @param pstbTabString StringBuffer 前置 Tab String
     * @param pstrTabCharecter String 以何種字元當成 Tab
     * @param pstrTagText String 要輸出的 Tag
     * @param pblnNewLine boolean 是否要換行
     * @sample outputPageTag(pbwrPrgNO,StringBuffer("\t\t"),"\t","<BODY>",true) return "\t\t\t"<br>
               outputPageTag(pbwrPrgNO,StringBuffer("\t\t\t"),"\t","</BODY>",true) return "\t\t"<br>
               outputPageTag(pbwrPrgNO,StringBuffer("\t\t"),"\t","<OPTION>TEST</OPTION>",true) return "\t\t"<br>

     */
    protected void outputPageTag(BufferedWriter pbwrOutput,
                                 StringBuffer pstbTabString,
                                 String pstrTabCharecter, String pstrTagText,
                                 int pintNewLineCount) {
        StringBuffer lstbOutputText = new StringBuffer(pstrTagText);
        try {
            //當輸出為tag 結束符號時，內縮次數減一次
            if (pstrTagText.indexOf("</") == 0) {
                pstbTabString.delete(0, pstrTabCharecter.length());
            }

            //輸出此Tag前是否需換行
            for (int i = 1; i <= pintNewLineCount; i++) {
                pbwrOutput.newLine();
            }
            /*if (pblnNewLine) {
                pbwrOutput.newLine();
                lstbOutputText.insert(0, pstbTabString.toString());
                         }*/

            pbwrOutput.write(lstbOutputText.toString());

            if (pstrTagText.indexOf("<") == 0 && pstrTagText.indexOf("</") < 0 &&
                pstrTagText.indexOf("<!") < 0 &&
                !(pstrTagText.indexOf("</") > 0 ||
                  pstrTagText.indexOf("/>") == pstrTagText.length() - 2)) {
                pstbTabString.append(pstrTabCharecter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lstbOutputText.delete(0, lstbOutputText.length() - 1);
            lstbOutputText = null;
        }
    }

    public Hashtable<String,String> get_edit_mode_css(String pstrRwMode, String pstrFldKind,
                                       String pstrOpenDB, String pstrGetFld,
                                       String pstrReadonlyYN,
                                       String pstrLinkKind) throws Exception {

        Hashtable<String,String> lhtResult = new Hashtable<String,String>();

        try {
            if (pstrRwMode.equals("H")) { //Hidden
                lhtResult.put("EDITMODE", "HIDDEN");
                lhtResult.put("CSS", "input");
            }

            if (pstrRwMode.equals("F")) {
                lhtResult.put("EDITMODE", "FILE");
                lhtResult.put("CSS", "input");
            }

            if (pstrRwMode.equals("T")) {
                lhtResult.put("EDITMODE", "TEXT");
                lhtResult.put("CSS",
                              ((pstrGetFld != null && !pstrGetFld.equals("")) ||
                               (pstrOpenDB != null &&
                                (pstrOpenDB.equals("DATE") ||
                                 pstrOpenDB.equals("DATETIME"))) ?
                               "input_window hand" : "input")
                        );
            }

            if (pstrRwMode.equals("B")) {
                lhtResult.put("EDITMODE", "BUTTON");
                lhtResult.put("CSS", "button b_normal");
            }

            if (pstrRwMode.equals("C")) {
                lhtResult.put("EDITMODE", "CHECKBOX");
                lhtResult.put("CSS", "checkbox");
            }

            if (pstrRwMode.equals("R")) {
                lhtResult.put("EDITMODE", "RADIO");
                lhtResult.put("CSS", "radio");
            }

            if (pstrRwMode.equals("L")) {
                lhtResult.put("EDITMODE", "LINK");
                lhtResult.put("CSS", "input_a");
            }

            if (pstrRwMode.equals("E")) {
                lhtResult.put("EDITMODE", "RESET");
                lhtResult.put("CSS", "button");
            }

            if (pstrRwMode.equals("S")) {
                lhtResult.put("EDITMODE", "SUBMIT");
                lhtResult.put("CSS", "button");
            }

            if (pstrRwMode.equals("A")) {
                lhtResult.put("EDITMODE", "NOTELINE");
                lhtResult.put("CSS", "text_head");
            }

            if (pstrRwMode.equals("D")) {
                lhtResult.put("EDITMODE", "SELECT");
                lhtResult.put("CSS", "select");
            }

            if (pstrRwMode.equals("M")) {
                lhtResult.put("EDITMODE", "TEXTAREA");
                lhtResult.put("CSS", "textarea");
            }

            if (pstrRwMode.equals("N")) {
                lhtResult.put("EDITMODE", "RICHTEXT");
                lhtResult.put("CSS", "textarea");
            }

            //僅有顯示一定會是readonly，在JspGenerator中若判斷是readonly自然會加 _noborder
            if (pstrRwMode.equals("V")) {
                lhtResult.put("EDITMODE", "TEXT");
                lhtResult.put("CSS", "input");
            }

            if (pstrReadonlyYN.equals("Y")) {
                lhtResult.put("CSS", (pstrLinkKind.equals("B") ? "input_lock" :
                                      "input_noborder"));

                if (pstrRwMode.equals("M") || pstrRwMode.equals("N")) {
                    lhtResult.put("CSS", "textarea_noborder");
                }
            }

            if (pstrFldKind.equals("N")) {
                //lhtResult.put("EDITMODE", "HIDDEN");
                lhtResult.put("CSS",
                              (new StringBuffer(lhtResult.get("CSS").toString())).
                              append(" num").toString());
            }

            return lhtResult;
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }


}
